package com.chen.serviceprice.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chen.internalcommon.constant.CommonStatusEnum;
import com.chen.internalcommon.dto.PriceRule;
import com.chen.internalcommon.dto.ResponseResult;
import com.chen.internalcommon.request.ForecastPriceRequest;
import com.chen.internalcommon.response.DirectionResponse;
import com.chen.internalcommon.response.ForecastPriceResponse;
import com.chen.internalcommon.util.Decimal;
import com.chen.serviceprice.mapper.PriceRuleMapper;
import com.chen.serviceprice.remote.ServiceMapClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author chenLiuPing
 **/
@Service
@Slf4j
public class ForecastPriceService {
    @Autowired
    private ServiceMapClient serviceMapClient;

    @Autowired
    private PriceRuleMapper priceRuleMapper;
    public ResponseResult forecastPrice(String depLongitude, String depLatitude, String destLongitude, String destLatitude,  String cityCode, String vehicleType) {
        log.info("forecastPrice start");
        ResponseResult<DirectionResponse> responseResult = serviceMapClient.driving(new ForecastPriceRequest(depLongitude, depLatitude, destLongitude, destLatitude, cityCode, vehicleType));
        DirectionResponse directionResponse = responseResult.getData();
        log.info("调用地图服务获取到的距离和时长为：{}", directionResponse);
        log.info("读取计价规则");
        LambdaQueryWrapper<PriceRule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PriceRule::getCityCode, cityCode)
                .eq(PriceRule::getVehicleType, vehicleType)
                .orderByDesc(PriceRule::getFareVersion);
        List<PriceRule> selectList = priceRuleMapper.selectList(queryWrapper);
        if (selectList.size() == 0) {
            return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EMPTY);
        }
        // 获取最大fareVersion的priceRule
        PriceRule priceRule = selectList.get(0);
        double price = getPrice(directionResponse.getDistance(), directionResponse.getDuration(), priceRule);
        String fareType = priceRule.getFareType();
        Integer fareVersion = priceRule.getFareVersion();
        log.info("根据距离和时长、计价规则计算出预估价格:{}", price);
        return ResponseResult.success(new ForecastPriceResponse(price, cityCode, vehicleType, fareType, fareVersion));

    }

    /**
     * 根据路程、时长、计价规则 算出总价
     * @param distance 路程 m
     * @param duration 时长 s
     * @param priceRule 计价规则
     * @return
     */
    private double getPrice(int distance, int duration, PriceRule priceRule) {
        // 总价钱 = 起步价 + (总里程 - 起步里程) * 每里程单价 + 总时长 * 时长单价
        Decimal price = new Decimal(priceRule.getStartFare());
        // 里程价
        double distancePrice = new Decimal(distance)
                .divide(1000.0)
                .subtract(priceRule.getStartMile())
                .multiply(priceRule.getUnitPricePerMile()).getValue();
        distancePrice = distancePrice < 0 ? 0 : distancePrice;

        // 时长价
        double durationPrice = new Decimal(duration)
                .divide(60.0)
                .multiply(priceRule.getUnitPricePerMinute()).getValue();
        return price.add(distancePrice)
                .add(durationPrice).getValue();
    }


}
